<?php
namespace Admin\Controller;

use Common\Util\UploadHandler;
use Common\Util\Page;
use Common\Controller\AdminbaseController;

class OperationController extends AdminbaseController {
	
	public function index() {
		$adminId = I('get.id');
		 
		$where = array();
		if ($adminId) {
			$where ['o.admin_id'] = $adminId;
		}
		
		$starTime = I('get.start_time');
		if ($starTime) {
			$endTime = I('get.end_time');
			$where ['o.created'] = array(array('gt', strtotime($starTime.' 00:00:00')));
			if ($endTime) {
				$endTimeStamp = strtotime($endTime.' 23:59:59');
			} else {
				$endTimeStamp = time();
			}
			$where ['o.created'] [] = array('lt', $endTimeStamp);
		}
		
		$operationModel = M('operation');
		$count = $operationModel->alias('o')->where($where)->count();
		
		$page = $this->page($count, 20);
		$list = $operationModel->alias('o')->field(array('o.*', 'au.username'))
		->join('LEFT JOIN '.C('DB_PREFIX').'admin_user as au on o.admin_id=au.id')
		->where($where)
		->limit($page->firstRow.','.$page->listRows)
		->select();
	
		$this->assign('list', $list);
		$this->assign('Page', $page->show('Admin'));
		$this->display('index');
	}
	
    public function loginRecord() {
    	
    	$starTime = I('get.start_time');
    	$where = array('o.action' => 'login');
    	
    	if ($starTime) {
    		$endTime = I('get.end_time');
    		$where ['o.created'] = array(array('gt', strtotime($starTime.' 00:00:00')));
    		if ($endTime) {
    			$endTimeStamp = strtotime($endTime.' 23:59:59');
    		} else {
    			$endTimeStamp = time();
    		}
    		$where ['o.created'] [] = array('lt', $endTimeStamp);
    	}
    	
    	$operationModel = M('operation');
    	$count = $operationModel->alias('o')->where($where)->count();
    	
    	$page = $this->page($count, 20);
    	
    	$list = $operationModel->alias('o')->field(array('o.*', 'au.username'))
    		->join('LEFT JOIN '.C('DB_PREFIX').'admin_user as au on o.admin_id=au.id')
    		->where($where)
    		->limit($page->firstRow.','.$page->listRows)
    	->select();
    	
    	$this->assign('list', $list);
    	$this->assign('Page', $page->show('Admin'));
    	$this->display('loginRecord');
    }
    
    public function operationAfterLogin() {
    	$adminId = I('get.id');
    	$time = I('get.time');
    	
    	$list = M('operation')->field(array('o.*', 'au.username'))
    	->join('o LEFT JOIN '.C('DB_PREFIX').'admin_user as au on o.admin_id=au.id')
    	->where(array('o.admin_id' => $adminId, 'o.created' => array(array('gt', $time), array('lt', time()))))->select();
    	 
    	$this->assign('list', $list);
    	$this->display('index');
    }
    
    public function addBank() {
    	if (!IS_POST) {
    		$this->display();
    	} else {
    		$data = I('post.');
    		
    		$data ['create_time'] = time();
    		$result = M('bank')->add($data);
    		if ($result) {
    			$operation = array();
    			\Think\Hook::listen('operation', $operation);
    			
    			$this->success('添加银行成功');
    		} else {
    			$this->error('添加银行失败');
    		}
    	}
    }
    
    public function uploadBankIcon() {
    	$options = C('BANK_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Bank/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    		 
    	$this->ajaxReturn($result [$param_name] [0]);
    }
    
    public function deleteBank() {
    	$id = I ('get.id');
    	$result = M('bank')->where(array('id' => $id))->save(array('status' => 1));
    	if ($result) {
    		$operation = array('operation_object_id' => $id);
    		\Think\Hook::listen('operation', $operation);
    		
    		$this->success('删除成功！');
    	} else {
    		$this->success('删除失败！');
    	}
    }
}